package com.sjgtw.cloud.web.jointDefenceUnit.service.impl;

import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.utils.DateUtils;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.core.utils.StringUtils;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.web.jointDefenceUnit.domain.JointDefenceUnit;
import com.sjgtw.cloud.web.jointDefenceUnit.mapper.JointDefenceUnitMapper;
import com.sjgtw.cloud.web.jointDefenceUnit.service.IJointDefenceUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 联防单位Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-09
 */
@Service
public class JointDefenceUnitServiceImpl implements IJointDefenceUnitService
{
    @Autowired
    private JointDefenceUnitMapper jointDefenceUnitMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询联防单位
     * 
     * @param id 联防单位主键
     * @return 联防单位
     */
    @Override
    public JointDefenceUnit selectJointDefenceUnitById(Long id)
    {
        return jointDefenceUnitMapper.selectJointDefenceUnitById(id);
    }

    /**
     * 查询联防单位列表
     * 
     * @param jointDefenceUnit 联防单位
     * @return 联防单位
     */
    @Override
    public List<JointDefenceUnit> selectJointDefenceUnitList(JointDefenceUnit jointDefenceUnit)
    {
        return jointDefenceUnitMapper.selectJointDefenceUnitList(jointDefenceUnit);
    }

    @Override
    public List<JointDefenceUnit> selectRemoteJointDefenceUnitList(JointDefenceUnit jointDefenceUnit) {
        return jointDefenceUnitMapper.selectRemoteJointDefenceUnitList(jointDefenceUnit);
    }

    /**
     * 新增联防单位
     * 
     * @param jointDefenceUnit 联防单位
     * @return 结果
     */
    @Override
    public int insertJointDefenceUnit(JointDefenceUnit jointDefenceUnit)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        jointDefenceUnit.setCreateBy(user.getUsername());
        jointDefenceUnit.setCreateTime(new Date());
        return jointDefenceUnitMapper.insertJointDefenceUnit(jointDefenceUnit);
    }

    /**
     * 修改联防单位
     * 
     * @param jointDefenceUnit 联防单位
     * @return 结果
     */
    @Override
    public int updateJointDefenceUnit(JointDefenceUnit jointDefenceUnit)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        jointDefenceUnit.setUpdateBy(user.getUsername());
        jointDefenceUnit.setUpdateTime(new Date());
        return jointDefenceUnitMapper.updateJointDefenceUnit(jointDefenceUnit);
    }

    /**
     * 批量删除联防单位
     * 
     * @param ids 需要删除的联防单位主键
     * @return 结果
     */
    @Override
    public int deleteJointDefenceUnitByIds(Long[] ids)
    {
        return jointDefenceUnitMapper.deleteJointDefenceUnitByIds(ids);
    }

    /**
     * 删除联防单位信息
     * 
     * @param id 联防单位主键
     * @return 结果
     */
    @Override
    public int deleteJointDefenceUnitById(Long id)
    {
        return jointDefenceUnitMapper.deleteJointDefenceUnitById(id);
    }

    @Override
    public void batchSave(List<JointDefenceUnit> entities) {
        if (entities != null && entities.size() > 0) {
            if (entities.size() >= 200) {
                int index = 0;
                while (index <= entities.size()) {
                    int last = Math.min(index + 200, entities.size());
                    List<JointDefenceUnit> temp = entities.subList(index, last );
                    if (temp != null && temp.size() > 0) {
                        jointDefenceUnitMapper.batchSave(temp);
                    }
                    index += 200;
                }
            } else {
                jointDefenceUnitMapper.batchSave(entities);
            }
        }
    }

    @Override
    public List<String> check(List<JointDefenceUnit> entities) {
        List<String> errors = new ArrayList<>();
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);

        for (JointDefenceUnit entity : entities) {
            if (StringUtils.isEmpty(entity.getName()) && StringUtils.isEmpty(entity.getLeader())
                    && StringUtils.isEmpty(entity.getPhone()) && StringUtils.isEmpty(entity.getMember())) {
                errors.add("序号为" + entity.getNum() + "行数据所有属性均无值");
            }
            if (user != null) {
                entity.setCreateBy(user.getUsername());
            }
            entity.setCreateTime(DateUtils.getNowDate());
        }
        return errors;
    }
}
